# -*- coding: utf-8 -*-
# @Time : 2021/9/10 15:55
# @Author : hello
# @Email : xxx@gmail.com
# @File : CRM_ST_KHGL_239.py
# @Project : crm

from selenium import webdriver
from time import sleep

from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select
from selenium.webdriver.common.alert import Alert
"""客户-验证添加功能-放入客户池"""

driver=webdriver.Chrome()
try:
    driver.implicitly_wait(30)
    driver.maximize_window()
    """已登录crm系统且进入客户展示界面"""
    cookies={'name':'PHPSESSID','value':'5o422r3lhfr2q9gmj0mmbfl2v7'}
    driver.get("http://192.168.1.155")
    driver.add_cookie(cookies)
    driver.get("http://192.168.1.155/crm/index.php?m=customer&a=index")

    clickadd = (By.CSS_SELECTOR, 'body > div.container > div.row > div:nth-child(1) > div > a')
    sleep(2)
    driver.find_element(*clickadd).click()

    fzrs = "admin"
    khmcs = "斯当鼎科技公司"
    ybs = "450000"
    synames = "张三"
    sychs = "先生"
    syyxs = "xx@xx.com"
    syzws = "业务经理"
    syqqs = "98799798"
    syphones = "15632455632"
    sybzs = "小型软件公司"
    fzbzs = "软件公司"
    xs = "河南省"
    ss = "郑州市"
    qs = "惠济区"
    jds = "东大街"

    fzr = (By.ID, 'owner_name')
    khmc = (By.ID, 'name')
    khhy = (By.ID, 'industry')  # 单选
    khxxly = (By.ID, 'origin')  # --下拉框
    companyxz = (By.ID, 'ownership2')  # 单选
    yb = (By.ID, 'zip_code')
    yearmy = (By.ID, 'annual_revenue')  # --下拉框
    pf = (By.ID, 'rating1')  # 单选
    adress_x = (By.NAME, "address['state']")
    adress_s = (By.NAME, "address['city']")
    adress_q = (By.NAME, "address['area']")
    jd = (By.NAME, "address['street']")
    # 首要联系人
    syname = (By.NAME, 'con_name')
    sych = (By.NAME, 'saltname')
    syyx = (By.NAME, 'con_email')
    syzw = (By.NAME, 'con_post')
    syqq = (By.NAME, 'con_qq')
    syphone = (By.NAME, 'con_telephone')
    sybz = (By.NAME, 'con_description')
    fzyg = (By.ID, 'no_of_employees')  # 下拉框
    fzbz = (By.ID, 'description')
    bc = (By.CSS_SELECTOR, 'input[value="保存"]')

    driver.find_element(*fzr).clear()
    driver.find_element(*fzr).send_keys(fzrs)
    driver.find_element(*khmc).send_keys(khmcs)
    driver.find_element(*khhy).click()
    khxx_locator = driver.find_element(*khxxly)
    Select(khxx_locator).select_by_visible_text('网络营销')
    driver.find_element(*companyxz).click()
    driver.find_element(*yb).send_keys(ybs)
    yearmy_locator = driver.find_element(*yearmy)
    Select(yearmy_locator).select_by_visible_text('10-20万')
    driver.find_element(*pf).click()
    x_locator = driver.find_element(*adress_x)
    Select(x_locator).select_by_visible_text(xs)
    s_locator = driver.find_element(*adress_s)
    Select(s_locator).select_by_visible_text(ss)
    q_locator = driver.find_element(*adress_q)
    Select(q_locator).select_by_visible_text(qs)
    driver.find_element(*jd).send_keys(jds)

    driver.find_element(*syname).send_keys(synames)
    driver.find_element(*sych).send_keys(sychs)
    driver.find_element(*syyx).send_keys(syyxs)
    driver.find_element(*syzw).send_keys(syzws)
    driver.find_element(*syqq).send_keys(syqqs)
    driver.find_element(*syphone).send_keys(syphones)
    driver.find_element(*sybz).send_keys(sybzs)
    yg_locator = driver.find_element(*fzyg)
    Select(yg_locator).select_by_visible_text('20-50人')
    driver.find_element(*fzbz).send_keys(fzbzs)
    sleep(3)
    driver.find_element(*bc).click()
    sleep(3)

    tables = (By.CSS_SELECTOR, "#form1 > table")
    tbodys = (By.TAG_NAME, "tbody")

    trs = (By.TAG_NAME, "tr")
    tds = (By.TAG_NAME, "td")
    a = (By.TAG_NAME, "a")
    table = driver.find_element(*tables)
    tbody = table.find_element(*tbodys)
    trss = tbody.find_elements(*trs)
    for tr in trss:
        td = tr.find_elements(*tds)
        if td[-1]:
            sleep(3)
            td[-1].find_elements(*a)[-1].click()
            break
    sleep(3)
#点击放入客户池
    khc=(By.CSS_SELECTOR,'input[value="放入客户池"]')
    driver.find_element(*khc).click()
    sleep(2)
    alert=Alert(driver)
    alert.accept()
    sleep(2)
    driver.find_element(*bc).click()

except Exception as e:
    raise e
finally:
    sleep(3)
    driver.quit()